//
//  RefundListViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/17.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "SerOrderSubListCell.h"
#import "RefundListModel.h"

#import "SerOrderSubSectionHeader.h"
#import "SerOrderSubSectionFooter.h"
#import "RefundDetailViewController.h"

#import "RefundListViewController.h"

@interface RefundListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray <RefundListModel *>*dataArray;

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, assign) NSInteger pageSize;

@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation RefundListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self refreshParam];
    [self request_handleRefund];
}

- (void)refreshParam {
    self.pageSize = 20;
    self.pageNum = 1;
}

- (void)initView {
    
    self.title = @"售后";
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 45;
}

#pragma mark sectionheader
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    SerOrderSubSectionHeader *headView = [[NSBundle mainBundle] loadNibNamed:@"SerOrderSubSectionHeader"owner:nil options:nil].firstObject;
    if (self.dataArray.count > section) {
        [headView fillHeaderWithModel:self.dataArray[section]];
    }
    return  headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    SerOrderSubSectionFooter *footerView = [[NSBundle mainBundle] loadNibNamed:@"SerOrderSubSectionFooter"owner:nil options:nil].firstObject;
    
    if (self.dataArray.count > section) {
        [footerView fillFooterWithModel:self.dataArray[section]];
    }
    
    footerView.SerOrderSubSectionFooterBlock = ^(NSInteger index) {
        
    };
    
    return  footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.dataArray.count > indexPath.section) {

        RefundDetailViewController *vc = [[RefundDetailViewController alloc] init];
        vc.rfId = self.dataArray[indexPath.section].rfId;
        vc.uosType = self.dataArray[indexPath.section].uosType;
        vc.uosOrderNo = self.dataArray[indexPath.section].uosOrderNo;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SerOrderSubListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderSubListCellID"];
    
    if (self.dataArray.count > indexPath.section) {
        [cell fillCellWithRFModel:self.dataArray[indexPath.section]];
    }
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}


- (NSMutableArray<RefundListModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderSubListCell" bundle:nil] forCellReuseIdentifier:@"SerOrderSubListCellID"];
        
        WEAKSELF
        _tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [weakSelf refreshParam];
            [weakSelf request_handleRefund];
        }];
//
        _tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            weakSelf.pageNum ++;
            [weakSelf request_handleRefund];
        }];
        _tableview.mj_footer.ignoredScrollViewContentInsetBottom = Is_IPhoneX ? Bottom_SafeHeight : CGFLOAT_MIN;

    }
    return _tableview;
}


- (void)request_handleRefund {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];
    NSMutableDictionary *t = @{}.mutableCopy;
    [t setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [t setValue:@"1" forKey:@"type"];
    [dic setValue:t forKey:@"t"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_RefundList];

    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在请求售后列表"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *list = [resultDic valueForKey:@"list"];
                
                if ([list isKindOfClass:[NSArray class]]) {

                    if (self.pageNum == 1) {
                        self.dataArray = @[].mutableCopy;
                    }
                    
                    for (int i = 0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        if ([listDic isKindOfClass:[NSDictionary class]]) {
                            RefundListModel *model = [RefundListModel modelWithDictionary:listDic];

                            NSArray *userOrderGoodsList = [listDic valueForKey:@"userOrderGoodsList"];
                            
                            if ([userOrderGoodsList isKindOfClass:[NSArray class]]) {
                                model.userOrderGoodsList = @[].mutableCopy;
                                
                                for (int j = 0; j < userOrderGoodsList.count; j++) {
                                    NSDictionary *userOrderGoodListDic = userOrderGoodsList[j];
                                    OrderGoodsModel *goodsModel = [OrderGoodsModel modelWithDictionary:userOrderGoodListDic];
                                    
                                    [model.userOrderGoodsList addObject:goodsModel];
                                }
                            }
                            //获取底部数据配置
                            [weakSelf.dataArray addObject:model];
                        }
                    }
                    
                    if (weakSelf.dataArray.count == 0) {
                        [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.tableview errorCode:kTTViewState_NoOrder btnTitle:@"" didPressButton:^{}];
                    }
                    [weakSelf.tableview reloadData];
                }
                
            } else {

                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }

    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}


@end
